package com.gin.kotlin.no_gay_alliance.database.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.RawQuery
import androidx.sqlite.db.SupportSQLiteQuery
import com.gin.kotlin.no_gay_alliance.database.entity.ReplyHistory
import com.gin.kotlin.no_gay_alliance.model.CountModel

@Dao
interface ReplyHistoryDao {
    /**
     * 添加/更新回复历史
     * @param list List<ReplyHistory>
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun add(list: List<ReplyHistory>)

    /**
     * 清空某一主题记录的所有回复
     * @param topicId Long
     */
    @Query("delete from ReplyHistory where topicId=:topicId")
    fun clearByTopicId(topicId: Long)

    /**
     * 清空某一用户的所有回复
     * @param authorUid Long
     */
    @Query("delete from ReplyHistory where authorUid=:authorUid")
    fun clearByUserId(authorUid: Long)

    /**
     * 按主题分组，统计回复数
     * @return List<ReportHistory>?
     */
    @Query("select count(1) as count,topicId as id from ReplyHistory where topicId in (:topicId) group by topicId")
    fun countByTopicId(topicId: Array<Long>): List<CountModel>?

    /**
     * 按作者id分组，统计回复数
     * @return List<ReportHistory>?
     */
    @Query("select count(1) as count,authorUid as id from ReplyHistory where authorUid in (:userId) group by authorUid")
    fun countByUserId(userId: Array<Long>): List<CountModel>?

    /**
     * 原生查询记录条数
     * @param supportSQLiteQuery SupportSQLiteQuery
     * @return Int
     */
    @RawQuery
    fun count(supportSQLiteQuery: SupportSQLiteQuery): Int

    /**
     * 原生查询记录
     * @param supportSQLiteQuery SupportSQLiteQuery
     * @return List<ReportHistory>?
     */
    @RawQuery
    fun list(supportSQLiteQuery: SupportSQLiteQuery): List<ReplyHistory>?

    /**
     * 按uuid查询回复历史
     * @param uuid Array<String>
     * @return List<ReplyHistory>?
     */
    @Query("select * from ReplyHistory where uuid in (:uuid)")
    fun list(uuid: Array<String>): List<ReplyHistory>?
}